package com.cn.bukeManage.service.serviceImpl;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import com.cn.bukeManage.dao.MemberDao;
import com.cn.bukeManage.dao.UserDao;
import com.cn.bukeManage.pojo.Customer;
import com.cn.bukeManage.pojo.Dealer;
import com.cn.bukeManage.pojo.Supplier;
import com.cn.bukeManage.pojo.User;
import com.cn.bukeManage.pojo.Worker;
import com.cn.bukeManage.pojo.page.Page;
import com.cn.bukeManage.service.MemberService;
@Service("memberService")
public class MemberServiceImpl implements MemberService {
	@Resource
	private MemberDao memberDao;
	@Resource
	private UserDao userDao;
	
	//得到所有的顾客按页码
	public List<Customer> getAllCustomerByPage(Model model,Integer currentPage) {
		Map<String,Object> map=new HashMap<>();
		Page page=new Page.PageBuilder().currentPage(currentPage).builder();
//		
//		User user=userDao.selectUserByName(String.valueOf(SecurityUtils.getSubject().getSession().getAttribute("userName")));
//		if(user !=null)
		map.put("userId",SecurityUtils.getSubject().getSession().getAttribute("userId"));
		page.setRows(memberDao.getAllCustomerByPage(map).size());
		model.addAttribute("page",page);
		map.put("begin",( currentPage-1)*page.getPageSize());
		map.put("pageSize", page.getPageSize());
		//当前登录用户
		List<Customer> customers=memberDao.getAllCustomerByPage(map);
		return customers;
	}
	
	
	//根据注册时间降序排序
	public List<Customer> getAllCustomerByPageJ(Model model, Integer currentPage) {
		Page page=new Page.PageBuilder().currentPage(currentPage).builder();
		List<Customer> customers=memberDao.getAllCustomerByPageJ(page);
		Integer rows=memberDao.getRowsOfCustomer();
		page.setRows(rows);
		model.addAttribute("page",page);
		return customers;
	}
	

	
	//根据注册时间升序排序
	public List<Customer> getAllCustomerByPageS(Model model, Integer currentPage) {
		Page page=new Page.PageBuilder().currentPage(currentPage).builder();
			List<Customer> customers=memberDao.getAllCustomerByPageS(page);
			Integer rows=memberDao.getRowsOfCustomer();
			page.setRows(rows);
			model.addAttribute("page",page);
			return customers;
		}
	//搜索顾客
	public List<Customer> searchCustomer(Model model, Integer currentPage,String search) {
		Page page=new Page.PageBuilder().currentPage(currentPage).builder();
		page.setSearch(search.trim());
		List<Customer> customers=memberDao.searchCustomer(page);
		Integer rows=memberDao.searchRowsOfCustomer(search.trim());
		page.setRows(rows);
		model.addAttribute("page",page);
		return customers;
	}
	//得到所有经销商的账号
	public List<Dealer> getAllDealerByPage(Model model, Integer currentPage) {
		Page page=new Page.PageBuilder().currentPage(currentPage).builder();
		Map<String,Object>map=new HashMap<>();
		map.put("userId", SecurityUtils.getSubject().getSession().getAttribute("userId"));
		Integer rows=memberDao.getAllDealerByPage(map).size();
		page.setRows(rows);
		model.addAttribute("page",page);		
		map.put("begin", (currentPage-1)*page.getPageSize());
		map.put("pageSize", page.getPageSize());
		List<Dealer> dealers=memberDao.getAllDealerByPage(map);
		return dealers;
	}
	//得到所有供货商
	public List<Supplier> getAllSupplierByPage(Model model, Integer currentPage) {
		Page page=new Page();
		page.setCurrentPage(currentPage);
		List<Supplier> suppliers=memberDao.getAllSupplierByPage(page);
		Integer rows=memberDao.getRowsOfSupplier();
		page.setRows(rows);
		model.addAttribute("page",page);	
		return suppliers;
	}
	//分页得到所有工人
	public List<Worker> getAllWorkerByPage(Model model, Integer currentPage) {
		Page page=new Page();
		page.setCurrentPage(currentPage);
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("YYYY/MM/dd HH:MM:SS");
		List<Worker> workers=memberDao.getAllWorkerByPage(page);
		for(int a=0;a<workers.size();a++){
			workers.get(a).setJoin_dateS(simpleDateFormat.format(workers.get(a).getJoin_date()));
		}
		Integer rows=memberDao.getRowsOfWorker();
		page.setRows(rows);
		model.addAttribute("page",page);			
		return workers;
	}
	//停用顾客或经销商子账号
	public Integer toUpdatecustomer(Integer customerId) {
		Integer integer = memberDao.toUpdatecustomer(customerId);
		return integer;
	}
	//根据注册时间降序排序经销商
	public List<Dealer> getAllDealerByPageJ(Model model, Integer currentPage) {
		Page page=new Page();
		page.setCurrentPage(currentPage);
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("YYYY/MM/dd HH:MM:SS");
		List<Dealer> dealers=memberDao.getAllDealerByPageJ(page);
		for(int a=0;a<dealers.size();a++){
			dealers.get(a).setJoin_dateS(simpleDateFormat.format(dealers.get(a).getJoin_date()));
		}
		Integer rows=memberDao.getRowsOfDealer();
		page.setRows(rows);
		model.addAttribute("page",page);		
		return dealers;
	}
	//根据注册时间进行升序排序
	public List<Dealer> getAllDealerByPageS(Model model, Integer currentPage) {
		Page page=new Page();
		page.setCurrentPage(currentPage);
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("YYYY/MM/dd HH:MM:SS");
		List<Dealer> dealers=memberDao.getAllDealerByPageS(page);
		for(int a=0;a<dealers.size();a++){
			dealers.get(a).setJoin_dateS(simpleDateFormat.format(dealers.get(a).getJoin_date()));
		}
		Integer rows=memberDao.getRowsOfDealer();
		page.setRows(rows);
		model.addAttribute("page",page);		
		return dealers;
	}
	//搜索经销商
	public List<Dealer> searchDealer(Model model,  String search) {
		Map<String,Object>map=new HashMap<>();
		map.put("search", search.trim());
		List<Dealer> dealers=memberDao.searchDealer(map);
		Integer rows=memberDao.searchRowsOfDealer(search.trim());
		Page page=new Page.PageBuilder().search(search).rows(rows).builder();
		model.addAttribute("page",page);		
		return dealers;
	}
	//停用经销商
	public Integer toUpdatedealer(Integer dealerId) {
		Integer integer = memberDao.toUpdatedealer(dealerId);
		return integer;
	}
	//根据注册时间降序排序工人
	public List<Worker> getAllWorkerByPageJ(Model model, Integer currentPage) {
		Page page=new Page();
		page.setCurrentPage(currentPage);
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("YYYY/MM/dd HH:MM:SS");
		List<Worker> workers=memberDao.getAllWorkerByPageJ(page);
		for(int a=0;a<workers.size();a++){
			workers.get(a).setJoin_dateS(simpleDateFormat.format(workers.get(a).getJoin_date()));
		}
		Integer rows=memberDao.getRowsOfWorker();
		page.setRows(rows);
		model.addAttribute("page",page);			
		return workers;
	}
	//根据注册时间升序排序工人
	public List<Worker> getAllWorkerByPageS(Model model, Integer currentPage) {
		Page page=new Page();
		page.setCurrentPage(currentPage);
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("YYYY/MM/dd HH:MM:SS");
		List<Worker> workers=memberDao.getAllWorkerByPageS(page);
		for(int a=0;a<workers.size();a++){
			workers.get(a).setJoin_dateS(simpleDateFormat.format(workers.get(a).getJoin_date()));
		}
		Integer rows=memberDao.getRowsOfWorker();
		page.setRows(rows);
		model.addAttribute("page",page);			
		return workers;
	}
	//搜索工人
	public List<Worker> searchWorker(Model model, Integer currentPage,String search) {
		Page page=new Page();
		page.setCurrentPage(currentPage);
		page.setSearch(search.trim());
		SimpleDateFormat simpleDateFormat=new SimpleDateFormat("YYYY/MM/dd HH:MM:SS");
		List<Worker> workers=memberDao.searchWorker(page);
		for(int a=0;a<workers.size();a++){
			workers.get(a).setJoin_dateS(simpleDateFormat.format(workers.get(a).getJoin_date()));
		}
		Integer rows=memberDao.searchRowsOfWorker(search.trim());
		page.setRows(rows);
		model.addAttribute("search",search.trim());
		model.addAttribute("page",page);			
		return workers;
	}
	//停用工人
	public Integer toUpdateworker(Integer workerId) {
		Integer integer = memberDao.toUpdateworker(workerId);
		return integer;
	}
	//根据id查询供货商信息
	public Supplier selectSupplier(Integer supplierId) {
		Supplier supplier = memberDao.selectSupplier(supplierId);
		return supplier;
	}
	//根据id修改供货商
	public Integer saveSupplier(Supplier supplier) {
		Integer integer = memberDao.saveSupplier(supplier);
		return integer;
	}
	//删除供货商
	public Integer deleteSupplier(Integer supplierId) {
		Integer integer = memberDao.deleteSupplier(supplierId);
		return integer;
	}
	//添加供货商
	public Integer insertSupplier(Supplier supplier) {
		Integer integer = memberDao.insertSupplier(supplier);
		return integer;
	}
	//搜索供货商
	public List<Supplier> searchSupplier(Model model, Integer currentPage, String search) {
		Page page=new Page();
		page.setCurrentPage(currentPage);
		page.setSearch(search);
		List<Supplier> suppliers=memberDao.searchSupplier(page);
		Integer rows=memberDao.searchRowsOfSupplier(search);
		page.setRows(rows);
		model.addAttribute("page",page);	
		return suppliers;
	}

	

}
